<?php
$localRows = array();
foreach (@$rows as $value){
    $items = array();
    foreach ($value[$model] as $key => $value) {
        if(!empty($_POST['searchBoxValue']) && $_POST['searchBoxName']==$key){
            $string = $value;
            $pattern = "/({$_POST['searchBoxValue']})(.*)/i";
            $replacement = '<span style=\'color:red;\'>${1}</span>${2}';
            $value = preg_replace($pattern, $replacement, $string);
            //var_dump($value,$pattern, $replacement, $string);
            //$value = str_ireplace($_POST['searchBoxValue'], "<span style='color:red;'>{$_POST['searchBoxValue']}</span>", $value);
        }
        $items[] = '"'.$key.'":"'. str_replace(array('"', "\n", "\r"), ' ', substr(strip_tags((is_array($value)?implode(",", $value):$value), '<span>'), 0, 100)).'"';
    }
    $localRows[] = "{".implode(",", $items)."}";
}
?>
{"total":"<?php echo $count; ?>","rows":[
    <?php
        echo implode(",", $localRows);
    ?>
]}